package com.example.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.sys.entity.User;
import com.example.sys.mapper.UserMapper;
import com.example.sys.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.sys.vo.UserSelectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author anan
 * @since 2019-06-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findUserByUserName(String userName) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", userName);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public Boolean updateLoginDate(User user) {
        User user1 = new User();
        userMapper.updateById(user);
        return null;
    }

    @Override
    public List<User> getUserByDeptId(String deptId) {
        List<User> list =userMapper.getUserByDeptId(deptId);
        return list;
    }

    @Override
    public List<User> getUserByDeptIds(String deptIds) {
        String[] arr=deptIds.split(",");
        List<String> list = new ArrayList<>();
        for (String str:arr) {
            list.add(str);
        }
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.in("dept_id", list);
        List<User> list1 = userMapper.selectList(wrapper);
        return list1;
    }

    @Override
    public IPage<User> selectPage(UserSelectVo userSelectVo) {
        IPage<User> iPage = new Page<>(userSelectVo.getPage(),userSelectVo.getLimit());
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (!StrUtil.hasEmpty(userSelectVo.getUserName()))
            wrapper.like("user_name", userSelectVo.getUserName());
        if (!StrUtil.hasEmpty(userSelectVo.getUserNick()))
            wrapper.like("user_nick", userSelectVo.getUserNick());
        if (!StrUtil.hasEmpty(userSelectVo.getPhonenumber()))
            wrapper.like("phonenumber", userSelectVo.getPhonenumber());
        return userMapper.selectPage(iPage, wrapper);
    }
}
